<?php


require 'Class/Qiniu/qiniu/rs.php';
require 'Class/Qiniu/qiniu/io.php';
require 'Class/Qiniu/qiniu/fop.php';
use \Qiniu_RS_PutPolicy;
use \Qiniu_MacHttpClient;
use \Qiniu_PutExtra;
use \Qiniu_RS_GetPolicy;
use \Qiniu_ImageInfo;
use \Qiniu_Exif;
use \Qiniu_ImageView;
// Home\Controller\Qiniu_MacHttpClient
class QiniuAction extends Action{

	public $bucket;
	public $accessKey;
	public $secretKey;
	public $client;

	public function _initialize(){
		$config_file = "data/conf/cloud.php";
		if(file_exists($config_file)){
			$config = include $config_file;
		}else{
			echo 'no config';exit;
		}
		// print_r($config);
// ./qrsboxcli init p4ruHomdvoaJNWkDwewpxPmdXdvgeNGNiPQjHApe vJkpgd_vfYs8uMZo5e6j4ToqD7uWJMOJ8PXypz3L sync missql []
		if(empty($config['qn_default_bucket']) || empty($config['qn_ak']) || empty($config['qn_sk'])) 
			die('empty config');
		$this->bucket    = $config['qn_default_bucket'];
		$this->accessKey = $config['qn_ak'];
		$this->secretKey = $config['qn_sk'];
		Qiniu_SetKeys($this->accessKey, $this->secretKey);
		$this->client = new Qiniu_MacHttpClient(null);
	}

	public function index(){
		// echo $this->imgInfo( 0 , 'lufei.jpg');
		// $this->imgExif( 0 , 'lufei.jpg');
		return $this->imgPreview( 0 , 'lufei.jpg' ,1,200,400);
	}

	public function getDomain( $bucket = '' ){
		$bucket = $bucket ? $bucket : $this->bucket;
		return $domain = $bucket.'.qiniudn.com';
	}

	public function getKeyUrl($key){
		$domain = $this->getDomain();
		return 'http://'.$domain.'/'.$key;
	}
	/**
	 * 查看单个文件属性
	 * @param  [type] $key [description]
	 * @return [type]      [description]
	 */
	public function info( $key ){
		list($ret, $err) = Qiniu_RS_Stat($this->client, $this->bucket, $key);
		if ($err !== null) {
		    return $err;
		} else {
		    return $ret;
		}
	}

	/**
	 * 复制单个文件
	 * @param  [type] $key  [description]
	 * @param  [type] $key1 [description]
	 * @return [type]       [description]
	 */
	public function copy( $key , $key1 ){
		$err = Qiniu_RS_Copy($this->client, $this->bucket, $key, $this->bucket, $key1);
		return $err;
	}

	/**
	 * 移动单个文件
	 * @param  [type] $key  [description]
	 * @param  [type] $key1 [description]
	 * @return [type]       [description]
	 */
	public function move( $key , $key1 ){
		$err = Qiniu_RS_Move($this->client, $this->bucket, $key, $this->bucket, $key1);
		return $err;
	}

	/**
	 * 删除单个文件
	 * @param  [type] $key [description]
	 * @return [type]      [description]
	 */
	public function del( $key ){
		$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
		return $err;
	}

	/**
	 * 服务端生成 上传凭证
	 * @return [type] [description]
	 */
	public function getToken(){
		$putPolicy = new Qiniu_RS_PutPolicy($this->bucket);
		$upToken = $putPolicy->Token(null);
		return $upToken;
	}

	/**
	 * 上传字符串
	 * @param  [type] $key [description]
	 * @param  string $str [description]
	 * @return [type]      [description]
	 */
	public function upStr( $key , $str = ''){
		$upToken = $this->getToken();
		list($ret, $err) = Qiniu_Put($upToken, $key, $str , null);
		if ($err !== null) {
		    return $err;
		} else {
		    return $ret;
		}
	}

	/**
	 * 上传本地文件
	 * @param  [type] $save_key [保存为]
	 * @param  [type] $file     [上传的文件]
	 * @return [type]           [description]
	 */
	public function up( $save_key , $file ){
		$upToken = $this->getToken();
		$putExtra = new Qiniu_PutExtra();
		$putExtra->Crc32 = 1;
		list($ret, $err) = Qiniu_PutFile($upToken, $save_key , $file , $putExtra);
		if ($err !== null) {
		    return $err;
		} else {
		    return $ret;
		}
	}

	/**
	 * 公有资源下载
	 * @param  string $bucket [description]
	 * @param  [type] $key    [description]
	 * @return [type]         [description]
	 */
	public function download( $bucket = '' , $key ){
		$bucket = $bucket ? $bucket : $this->bucket;
		$domain = $bucket.'.qiniudn.com';
		//$baseUrl 就是您要访问资源的地址
		$baseUrl = Qiniu_RS_MakeBaseUrl($domain, $key);
		return $baseUrl;
	}

	/**
	 * 私有资源下载
	 * @param  string $bucket [description]
	 * @param  [type] $key    [description]
	 * @return [type]         [description]
	 */
	public function private_download( $bucket = '' , $key ){
		$bucket = $bucket ? $bucket : $this->bucket;
		$domain = $bucket.'.qiniudn.com';
		$baseUrl = Qiniu_RS_MakeBaseUrl($domain, $key);
		$getPolicy = new Qiniu_RS_GetPolicy();
		$privateUrl = $getPolicy->MakeRequest($baseUrl, null);
		return $privateUrl;
	}

	/**
	 * 查看图像属性
	 * @param  string $bucket [description]
	 * @param  [type] $key    [description]
	 * @return [type]         [description]
	 */
	public function imgInfo( $bucket = '' , $key ){
		$bucket = $bucket ? $bucket : $this->bucket;
		$domain = $bucket.'.qiniudn.com';
		$baseUrl = Qiniu_RS_MakeBaseUrl($domain, $key);
		//生成fopUrl
		$imgInfo = new Qiniu_ImageInfo;
		$imgInfoUrl = $imgInfo->MakeRequest($baseUrl);
		//对fopUrl 进行签名，生成privateUrl。 公有bucket 此步可以省去。
		$getPolicy = new Qiniu_RS_GetPolicy();
		$imgInfoPrivateUrl = $getPolicy->MakeRequest($imgInfoUrl, null);
		$json = file_get_contents($imgInfoPrivateUrl);
		return $json;
	}

	/**
	 * 查看图片EXIF信息
	 * @param  string $bucket [description]
	 * @param  [type] $key    [description]
	 * @return [type]         [description]
	 */
	public function imgExif( $bucket = '' , $key ){
		$bucket = $bucket ? $bucket : $this->bucket;
		$domain = $bucket.'.qiniudn.com';
		$baseUrl = Qiniu_RS_MakeBaseUrl($domain, $key);
		//生成fopUrl
		$imgExif = new Qiniu_Exif;
		$imgExifUrl = $imgExif->MakeRequest($baseUrl);
		//对fopUrl 进行签名，生成privateUrl。 公有bucket 此步可以省去。
		$getPolicy = new Qiniu_RS_GetPolicy();
		$imgExifPrivateUrl = $getPolicy->MakeRequest($imgExifUrl, null);
		// echo "====> imageView privateUrl: \n";
		// echo $imgExifPrivateUrl . "\n";
		return $imgExifPrivateUrl;
	}

	public function imgPreview( $bucket = '' , $key , $mode=1,$width='60',$height='120'){
		$bucket = $bucket ? $bucket : $this->bucket;
		$domain = $bucket.'.qiniudn.com';
		$baseUrl = Qiniu_RS_MakeBaseUrl($domain, $key);
		//生成fopUrl
		$imgView = new Qiniu_ImageView;
		$imgView->Mode = $mode;
		$imgView->Width = $width;
		$imgView->Height = $height;
		$imgViewUrl = $imgView->MakeRequest($baseUrl);
		//对fopUrl 进行签名，生成privateUrl。 公有bucket 此步可以省去。
		$getPolicy = new Qiniu_RS_GetPolicy();
		$imgViewPrivateUrl = $getPolicy->MakeRequest($imgViewUrl, null);
		// echo "====> imageView privateUrl: \n";
		// echo $imgViewPrivateUrl . "\n";
		return $imgViewPrivateUrl; 

	}

	/**
	 * 云存储管理删除文件
	 * @return [type] [description]
	 */
	public function file_del(){
// Array
// (
//     [onethink_admin] => Array
//         (
//             [user_auth] => Array
//                 (
//                     [uid] => 1
//                     [username] => missql
//                     [last_login_time] => 1396485314
//                 )

//             [user_auth_sign] => 5fc8719eb4012ec046fab1d8c65a73643cb260b9
//         )

// )
		// print_r($_SESSION);
		$bool = isset($_SESSION['onethink_admin']['user_auth']['uid']);
		$id = I('post.id');
		$key = M('cloudfile')->where('id='.$id)->getField('key');
		// echo $key;
		$res = $this->del($key);
		// var_dump($res);
		if(!$res){
			echo 1;
			M('cloudfile')->where('id='.$id)->delete();
		}else{
			echo json_encode($res);
		}
	}
}	// END